% function for reading an rts file 18.07.2000
% returns structured array TS and file pointer % 10.10.2002
% usage [TS,fid] = rts_in(fid); 
%latest date 5.12.2002


function[TS,fid] = rts_in(fid),

[nchannel,Count]  = fread(fid,1,'ushort',2);
TS.head.nchannel = nchannel;
[MaxRecord,Count] = fread(fid,1,'unsigned long');
[Record2,Count] = fread(fid,1,'unsigned long');
[Record3,Count] = fread(fid,1,'unsigned long');
[Record4,Count] = fread(fid,1,'unsigned long');
[Record5,Count] = fread(fid,1,'unsigned long',24);

[Bdst,Count] = fread(fid,[16 4],'uchar');

[Exd, Count] = fread(fid,8,'uchar');
[Eyd, Count] = fread(fid,8,'uchar');
[dummy,Count] = fread(fid,56,'uchar');
[Filt,Count]  = fread(fid,[5 1],'ushort');
TS.filter.Filt = Filt; % filter status
[dummy,Count] = fread(fid,10,'uchar');
[TS.head.SiteID,Count]= fread(fid,4,'uchar'); % site ID
[TS.head.Lat,Count]=fread(fid,16,'uchar');
[TS.head.Log,Count]=fread(fid,16,'uchar');
[dummy,Count] = fread(fid,16,'uchar');
[Exa, Count] = fread(fid,4,'uchar');
[Eya, Count] = fread(fid,4,'uchar');
[Hxa, Count] = fread(fid,4,'uchar');
[Hya, Count] = fread(fid,4,'uchar');
[Hza, Count] = fread(fid,4,'uchar');
[dummy,Count] = fread(fid,492,'uchar');
[Record1,Count] = fread(fid,1,'unsigned long');
[dummy,Count] = fread(fid,12,'uchar');
[Bdst(:,5),Count] = fread(fid,16,'uchar');
[Bden,Count] = fread(fid,[16 5],'uchar');
[Events,Count]  = fread(fid,[1 5],'ushort');
[hpf,Count]  = fread(fid,1,'ushort');
[TS.filter.NotchF,Count]  = fread(fid,[5 1],'float');
[dummy,Count] = fread(fid,68,'uchar');
[TS.head.fs,count] = fread(fid,1,'float');
[dummy,Count] = fread(fid,40,'uchar');

if sum([Record1,Record2,Record3,Record4,Record5]) ~= max([Record1,Record2,Record3,Record4,Record5]),
   TS.msg = 'The file contains more than one band';
   TS.err = -1;
else,
    TS.err = 0;
end;

Bddm(:,1) = Bdst(:,5);
Bddm(:,2) = Bdst(:,1);
Bddm(:,3) = Bdst(:,2);
Bddm(:,4) = Bdst(:,3);
Bddm(:,5) = Bdst(:,4);
Bdst = Bddm;


NBlocks = max([Record1,Record2,Record3,Record4,Record5]);

nband = find([Record1,Record2,Record3,Record4,Record5] > 0); 
TS.head.band = nband;
nlines =( (NBlocks/nchannel) * 256 ) / 4;
array = [nlines nchannel];
[TS.matrix,Count] = fread(fid,array,'long');

if Count~= nlines*nchannel,
fprintf('Error in source file reading\n');
return;
end;


Exd = str2num(setstr(Exd'));			%e - probe spacing
Eyd = str2num(setstr(Eyd'));
Exa = str2num(setstr(Exa'));			%channel amplification
Eya = str2num(setstr(Eya'));
Hxa = str2num(setstr(Hxa'));
Hya = str2num(setstr(Hya'));
Hza = str2num(setstr(Hza'));

TS.probe.dist = [Exd Eyd Eyd Eyd Eyd];
TS.probe.gain = [Exa Eya Hxa Hya Hza];

Bdstart = setstr(Bdst(:,nband))';
Bdend   = setstr(Bden(:,nband))';

SBS = size(Bdstart);
SBE = size(Bdend);
	
a = str2num([Bdstart(7:8) Bdstart(1:2) Bdstart(4:5) Bdstart(9:10) Bdstart(12:13) Bdstart(15:16)]);

day = str2num(Bdstart(4:5));
hr = str2num(Bdstart(9:10));
mi = str2num(Bdstart(12:13));
se = str2num(Bdstart(15:16));
	
TS.head.StID = day*24*3600 + hr*3600 + mi*60 + se; % time ID
TS.head.Bdstart = Bdstart;
TS.head.Bdend = Bdend;
